package com.tssup.fesco.search.utils;

import com.tssup.fesco.search.vo.ManageVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 *
 * @author jiashen
 * @date 17-5-25
 */
public class SearchUtils {
    @Value("${search.approve.secretKey}")
    private String secretKey;

    public ManageVo getManageVo() {

        Long timestamp = System.currentTimeMillis() / 1000;
        String sing = DigestUtils.md5Hex(secretKey + timestamp.toString());

        ManageVo manageVo = new ManageVo();
        manageVo.setSign(sing);
        manageVo.setTimestamp(timestamp);
        return manageVo;
    }

    public static String formatPrice(BigDecimal price) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(price.setScale(2, BigDecimal.ROUND_DOWN));
    }

    public static String getFirstChar(String inputStr) {
        if (StringUtils.isBlank(inputStr)) {
            return inputStr;
        }
        char firstChar = inputStr.charAt(0);
        if ((firstChar <= 'Z' && firstChar >= 'A')
            || (firstChar <= 'z' && firstChar >= 'a')) {
            return inputStr.substring(0, 1).toUpperCase();
        }
        return "#";
    }
}
